
package kaizone.haining.chaobohui.fragment;

import java.util.ArrayList;
import java.util.List;

import kaizone.haining.chaobohui.R;
import kaizone.haining.chaobohui.activity.CBHActivity;
import kaizone.haining.chaobohui.activity.DetailActivity;
import kaizone.haining.chaobohui.json.Data;
import kaizone.haining.chaobohui.json.Item;
import kaizone.haining.chaobohui.json.UserCommentDO;
import kaizone.haining.chaobohui.json.UserCommentItem;
import kaizone.haining.chaobohui.json.UserFollowDO;
import kaizone.haining.chaobohui.json.UserFollowItem;
import kaizone.haining.chaobohui.json.Item.PicModel;
import kaizone.haining.chaobohui.login.UserControl;
import kaizone.haining.chaobohui.net.DataControl;
import kaizone.haining.chaobohui.util.ImageFetcher;
import kaizone.haining.chaobohui.widget.CBHItemHuodong;
import kaizone.haining.chaobohui.widget.DetailGridImage;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ListView;
import android.widget.TextView;

public class DetailUsersFragment extends Fragment implements DataControl.IDataUpdateListener,
        OnItemClickListener {

    public static final String USERS_FLAG = "users_flag";

    public static final int MY_ACT_FOLLOWS = 0x0004; // 关注活动

    public static final int MY_ACT_CHECKINS = 0x0005; // 参加的活动

    public static final int MY_SUBJ_FOLLOWS = 0x0006; // 我收藏的专题

    public static final int FLAG_NOTIFICE = 0x0007; // 我收到通知

    private int mFlag;

    private String mDataSourceUrl;

    private List<Object> mData;

    private UserAdapter mAdapter;

    private DataControl mDataControl;

    private UserControl mUserControl;

    private ListView mListView;

    private GridView mGridView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mFlag = getArguments() != null ? getArguments().getInt(USERS_FLAG) : 0;
        initDataSource();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = (View) inflater.inflate(R.layout.detail_users, null);
        mListView = (ListView) view.findViewById(R.id.listview);
        mGridView = (GridView) view.findViewById(R.id.gridview);

        mAdapter = new UserAdapter();
        mListView.setAdapter(mAdapter);
        mListView.setCacheColorHint(0);
        mListView.setOnItemClickListener(this);

        mGridView.setAdapter(mAdapter);
        mGridView.setOnItemClickListener(this);
        initContent();
        initDataControl();

        return view;
    }

    private void initDataControl() {
        mDataControl = new DataControl(getActivity());
        mDataControl.addChangeListener(this);

        mDataControl.exec(mDataSourceUrl);
    }

    private void initDataSource() {
        mData = new ArrayList<Object>();
        mUserControl = new UserControl(getActivity());

        if (mFlag == MY_ACT_FOLLOWS) {
            mDataSourceUrl = mUserControl.requestMyActFollowsString();
        } else if (mFlag == MY_ACT_CHECKINS) {
            mDataSourceUrl = mUserControl.requestMyActCheckinsString();
        } else if (mFlag == MY_SUBJ_FOLLOWS) {
            mDataSourceUrl = mUserControl.requestMySubjFollowsString();
        } else if (mFlag == FLAG_NOTIFICE) {
            mDataSourceUrl = mUserControl.requestMyComments();
        }
    }

    private void initContent() {
        if (mFlag == MY_ACT_FOLLOWS) {
            mGridView.setVisibility(View.GONE);
            mListView.setVisibility(View.VISIBLE);
        } else if (mFlag == MY_ACT_CHECKINS) {
            mGridView.setVisibility(View.GONE);
            mListView.setVisibility(View.VISIBLE);
        } else if (mFlag == MY_SUBJ_FOLLOWS) {
            mGridView.setVisibility(View.GONE);
            mListView.setVisibility(View.VISIBLE);
        } else if (mFlag == FLAG_NOTIFICE) {
            mGridView.setVisibility(View.GONE);
            mListView.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public Object backParse(String datastring, String requeststring) {
        if (TextUtils.isEmpty(datastring))
            return null;
        if (TextUtils.isEmpty(requeststring))
            return null;
        //
        if (requeststring.equals(mUserControl.requestMyActCheckinsString())) {
            UserFollowDO userFollowDO = UserFollowDO.parseJson(datastring);
            return userFollowDO;
        }//
        else if (requeststring.equals(mUserControl.requestMyActFollowsString())) {
            UserFollowDO userFollowDO = UserFollowDO.parseJson(datastring);
            return userFollowDO;
        }//
        else if (requeststring.equals(mUserControl.requestMySubjFollowsString())) {
            UserFollowDO userFollowDO = UserFollowDO.parseJson(datastring);
            return userFollowDO;
        }//
        else if (requeststring.equals(mUserControl.requestMyComments())) {
            UserCommentDO userCommentDO = UserCommentDO.parseJson(datastring);
            return userCommentDO;
        }

        return null;
    }

    @Override
    public void foreUpdate(Object result, String requeststring) {
        if (result == null)
            return;
        if (TextUtils.isEmpty(requeststring))
            return;
        if (requeststring.equals(mUserControl.requestMyActFollowsString())) {
            if (result instanceof UserFollowDO) {
                UserFollowDO userFollowDO = (UserFollowDO) result;
                Data data = userFollowDO.getData();
                if (data == null)
                    return;
                List<UserFollowItem> items = (List<UserFollowItem>) data.getItems();
                mData.removeAll(mData);
                mData.addAll(items);
                mAdapter.notifyDataSetChanged();
            }
        }//
        else if (requeststring.equals(mUserControl.requestMySubjFollowsString())) {
            if (result instanceof UserFollowDO) {
                UserFollowDO userFollowDO = (UserFollowDO) result;
                Data data = userFollowDO.getData();
                if (data == null)
                    return;
                List<UserFollowItem> items = (List<UserFollowItem>) data.getItems();
                mData.removeAll(mData);
                mData.addAll(items);
                mAdapter.notifyDataSetChanged();
            }
        }//
        else if (requeststring.equals(mUserControl.requestMyActCheckinsString())) {
            if (result instanceof UserFollowDO) {
                UserFollowDO userFollowDO = (UserFollowDO) result;
                Data data = userFollowDO.getData();
                if (data == null)
                    return;
                List<UserFollowItem> items = (List<UserFollowItem>) data.getItems();
                mData.removeAll(mData);
                mData.addAll(items);
                mAdapter.notifyDataSetChanged();
            }
        } else if (requeststring.equals(mUserControl.requestMyComments())) {
            if (result instanceof UserCommentDO) {
                UserCommentDO userCommentDO = (UserCommentDO) result;
                Data data = userCommentDO.getData();
                if (data == null)
                    return;
                List<UserCommentItem> items = (List<UserCommentItem>) data.getItems();
                mData.removeAll(mData);
                mData.addAll(items);
                mAdapter.notifyDataSetChanged();
            }
        }
    }

    @Override
    public void exception(Exception e) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Object data = mData.get(position);
        if (data instanceof UserFollowItem) {
            UserFollowItem userFollowItem = (UserFollowItem) data;
            if (mFlag == MY_ACT_FOLLOWS) {
                final int activityid = userFollowItem.getActivityId();
                DetailActivity.come(getActivity(), DetailActivity.FLAG_HUODONG, activityid);
            }//
            else if (mFlag == MY_ACT_CHECKINS) {
                final int activityid = userFollowItem.getActivityId();
                DetailActivity.come(getActivity(), DetailActivity.FLAG_HUODONG, activityid);
            } else if (mFlag == MY_SUBJ_FOLLOWS) {
                final int subjectid = userFollowItem.getSubjectId();
                // DetailActivity.openSubject(getActivity(), "", subjectid);
            }
        }

    }

    private class UserAdapter extends BaseAdapter {
        @Override
        public int getCount() {
            return mData != null ? mData.size() : 0;
        }

        @Override
        public Object getItem(int position) {
            return mData != null ? mData.get(position) : 0;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            //
            View view = null;
            switch (mFlag) {
                case MY_ACT_CHECKINS:
                case MY_ACT_FOLLOWS:
                case MY_SUBJ_FOLLOWS:
                case FLAG_NOTIFICE:
                    view = getView1(position, convertView, parent);
                    break;
                default:
                    break;
            }
            return view;
        }
    }

    public View getView1(int position, View convertView, ViewGroup parent) {
        TextView textView = null;
        if (convertView == null) {
            textView = new TextView(getActivity());
            convertView = textView;
        }
        textView = (TextView) convertView;
        Object data = mData.get(position);
        if (data instanceof UserFollowItem) {
            final UserFollowItem userFollowItem = (UserFollowItem) data;
            textView.setTextSize(20);
            textView.setText(userFollowItem.getTargetName());
        } else if (data instanceof UserCommentItem) {
            final UserCommentItem userCommentItem = (UserCommentItem) data;

            textView.setText("我：" + userCommentItem.getContent());
            textView.append("\n");
            textView.append(userCommentItem.getTargetName());
        }
        return textView;
    }

    public View getView2(int position, View convertView, ViewGroup parent) {
        CBHItemHuodong cbhItemView = null;
        if (convertView == null) {
            convertView = (CBHItemHuodong) View.inflate(getActivity(), R.layout.item_huodong, null);
            if (getActivity() instanceof CBHActivity) {
                CBHActivity cbhActivity = (CBHActivity) getActivity();
                ((CBHItemHuodong) convertView).setImageFetcher(cbhActivity.getImageFetcher());
            }
        }
        cbhItemView = (CBHItemHuodong) convertView;
        Object data = mData.get(position);
        if (data instanceof Item) {
            cbhItemView.setData((Item) data);
        }
        return cbhItemView;
    }

    public View getView3(int position, View convertView, ViewGroup parent) {
        DetailGridImage gridImage = null;
        if (convertView == null) {
            gridImage = (DetailGridImage) View.inflate(getActivity(), R.layout.detail_grid_img,
                    null);
            convertView = gridImage;
        }
        gridImage = (DetailGridImage) convertView;
        // test data...
        int size = mData.size();
        if (position < size) {
            Object data = mData.get(position);
            if (data instanceof PicModel) {
                PicModel picModel = (PicModel) data;
                if (picModel != null) {
                    gridImage.setLevel("" + position);
                    gridImage.setDescription(picModel.path);
                    if (getActivity() instanceof CBHActivity) {
                        CBHActivity cbhActivity = (CBHActivity) getActivity();
                        ImageFetcher imageFetcher = cbhActivity.getImageFetcher();
                        gridImage.loadImage(picModel.sum100x000, imageFetcher);
                    }
                }
            }
        }
        return gridImage;
    }

    public static DetailUsersFragment newInstance(int flag) {
        final DetailUsersFragment f = new DetailUsersFragment();

        final Bundle args = new Bundle();
        args.putInt(USERS_FLAG, flag);
        f.setArguments(args);

        return f;
    }

}
